"""
服务模型
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, DECIMAL, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from extension import db


class ServiceType(db.Model):
    """服务类型表"""
    __tablename__ = 'service_type'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False, unique=True, comment='类型名称')
    description = Column(Text, comment='类型描述')
    created_at = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    services = relationship('Service', back_populates='type', cascade='all, delete-orphan')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建实例"""
        return cls(
            name=data.get('name'),
            description=data.get('description')
        )


class Service(db.Model):
    """服务表"""
    __tablename__ = 'service'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False, comment='服务名称')
    price = Column(DECIMAL(10, 2), nullable=False, comment='服务价格')
    description = Column(Text, comment='服务描述')
    type_id = Column(Integer, ForeignKey('service_type.id'), nullable=True, comment='服务类型ID')
    created_at = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    type = relationship('ServiceType', back_populates='services')
    
    def to_dict(self, include_relations=True):
        """转换为字典"""
        result = {
            'id': self.id,
            'name': self.name,
            'price': float(self.price) if self.price else 0,
            'description': self.description,
            'type_id': self.type_id,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }
        
        # 如果包含关联对象，添加类型信息
        if include_relations and self.type:
            result['type_name'] = self.type.name
        
        return result
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建实例"""
        return cls(
            name=data.get('name'),
            price=data.get('price', 0),
            description=data.get('description'),
            type_id=data.get('type_id')
        )
    
    def update_from_dict(self, data):
        """从字典更新实例"""
        if 'name' in data:
            self.name = data['name']
        if 'price' in data:
            self.price = data['price']
        if 'description' in data:
            self.description = data['description']
        if 'type_id' in data:
            self.type_id = data['type_id']
        return self

